<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-row :gutter="20">
        <el-col :span="24" align="right">
          <el-form-item label="物料编码" prop="ksCode">
            <el-input
              v-model="queryParams.cpCode"
              placeholder="请输物料编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          icon="el-icon-consume"
          :disabled="multiple"
          @click="handleConsume"
          v-hasPermi="['warehouse:cpkc:consume']"
        >消耗记录</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          size="mini"
          icon="el-icon-increase"
          :disabled="multiple"
          @click="handleIncrease"
          v-hasPermi="['warehouse:cpkc:increase']"
        >入库记录</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          size="mini"
          icon="el-icon-all"
          :disabled="multiple"
          @click="handleInventory"
          v-hasPermi="['warehouse:cpkc:inventory']"
        >库存详情</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cpkcList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品编码" align="center" prop="cpCode" />
      <el-table-column label="产品名称" align="center" prop="cpProduct.cpName" />
      <el-table-column label="产品品牌" align="center" prop="cpProduct.cpppProductBrand.ppName" />
      <el-table-column label="产品分类" align="center" prop="cpProduct.cpflProductClassification.cpflName"></el-table-column>
      <el-table-column label="产品型号" align="center" prop="cpProduct.cpType" />
      <el-table-column label="产品类型" align="center" prop="cpProduct.cpLxcode" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.product_type" :value="scope.row.cpProduct.cpLxcode"/>
          </template>
      </el-table-column>
      <el-table-column label="库存数量" align="center" prop="havenum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-consume"
            @click="handleConsume(scope.row)"
            v-hasPermi="['warehouse:cpkc:consume']"
          >消耗记录</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-increase"
            @click="handleIncrease(scope.row)"
            v-hasPermi="['warehouse:cpkc:increase']"
          >入库记录</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-all"
            @click="handleInventory(scope.row)"
            v-hasPermi="['warehouse:cpkc:inventory']"
          >库存详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--     弹框 -->
    <el-dialog :title="PopUpsTitle" :visible.sync="open1" width="45%">
      <el-container>
        <el-header>
          <el-input suffix-icon="el-icon-search" @keyup.enter.native="getPeoValDirector"></el-input>
        </el-header>
        <!--展示入库记录-->
        <el-main v-if="increaseList !== null">
          <el-table  border :data="increaseList" style="text-align: center;">
            <el-table-column prop="rkCode" label="入库单号" width="180px"></el-table-column>
            <el-table-column prop="cppcCode" label="产品批次号"></el-table-column>
            <el-table-column prop="ckWarehouse.ckName" label="入库仓库"></el-table-column>
            <el-table-column label="入库时间" align="center" prop="rkTime" width="140">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.rkTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="rkMan" label="入库人员"></el-table-column>
            <el-table-column prop="rkNum" label="入库数量"></el-table-column>
          </el-table>
          <pagination
            v-show="queryParams1.total>0"
            :total="queryParams1.total"
            :page.sync="queryParams1.pageNum"
            :limit.sync="queryParams1.pageSize"
            @pagination="handleIncrease"
          />
        </el-main>
        <!--展示库存详情-->
        <el-main v-if="inventoryList !== null">
          <el-table  border :data="inventoryList" style="text-align: center;">
            <el-table-column prop="cpProduct.cpName" label="产品名称" ></el-table-column>
            <el-table-column prop="cpProduct.cpType" label="产品型号"></el-table-column>
            <el-table-column prop="ckCode" label="仓库编码"></el-table-column>
            <el-table-column prop="ckWarehouse.ckName" label="仓库名称"></el-table-column>
            <el-table-column prop="havenum" label="库存数量"></el-table-column>
          </el-table>
          <pagination
            v-show="queryParams1.total>0"
            :total="queryParams1.total"
            :page.sync="queryParams1.pageNum"
            :limit.sync="queryParams1.pageSize"
            @pagination="handleInventory"
          />
        </el-main>
        <!--展示消耗记录-->
        <el-main v-if="consumeList !== null ">
          <el-table  border :data="consumeList" style="text-align: center;">
            <el-table-column label="发货单号" align="center" prop="fhjlShippingRecordForm.fhCode" width="160px"></el-table-column>
            <el-table-column label="产品批次" align="center" prop="cppcCode" width="120px"></el-table-column>
            <el-table-column label="仓库名称" align="center" prop="ckWarehouse.ckName"></el-table-column>
            <el-table-column label="客商名称" align="center" prop="fhjlShippingRecordForm.ksMerchants.merchantsName"></el-table-column>
            <el-table-column label="发货数量" align="center" prop="fhNum"></el-table-column>
            <el-table-column label="发货人" align="center" prop="fhjlShippingRecordForm.fhPeo"></el-table-column>
            <el-table-column label="发货时间" align="center" prop="fhjlShippingRecordForm.fhTime" width="120px">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.fhjlShippingRecordForm.fhTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="queryParams1.total>0"
            :total="queryParams1.total"
            :page.sync="queryParams1.pageNum"
            :limit.sync="queryParams1.pageSize"
            @pagination="handleConsume"
          />
        </el-main>
      </el-container>
    </el-dialog>

  </div>
</template>

<script>
import {increaseList, inventoryList, listCpkc,consumeList} from "@/api/warehouse/cpkc";

export default {
  name: "ylkc",
  dicts: ['wl_from', 'is_use', 'wl_type', 'product_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWldaMaterialFile: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 原料库存表格数据
      cpkcList: [],
      //库存详情
      inventoryList:null,
      //来料记录
      increaseList:null,
      //消耗记录
      consumeList:null,
      // 弹出层标题
      PopUpsTitle: null,
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（客商选择）
      open1: false,
      // 弹床标题

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cpCode: null,
        wlCode: null
      },
      // 查询客商参数
      queryParams1: {
        pageNum: 1,
        pageSize: 10,
        total:0,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询来料入库列表 */
    getList() {
      this.reset();
      this.loading = true;
      listCpkc(this.queryParams).then(response => {
        this.cpkcList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        llCode: null,
        ksCode: null,
        wlCode: null,
        getTime: null,
        isUse: null,
        isDelete: null,
        total: null,
        wldaMaterialFile: {
          wlName:null,
          wlCode:null
        },
        ksMerchants:{
          ksCode:null,
          merchantsName:null
        }
      };
      this.consumeList=null;
      this.increaseList=null;
      this.inventoryList=null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cpCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加来料入库";
    },
    /** 消耗记录按钮操作 */
    handleConsume(row) {
      this.reset()
      this.PopUpsShow()
      const wlCodes = row.cpCode || this.ids
      consumeList(wlCodes).then(response => {
        this.consumeList=response.rows
        this.title = "消耗记录";
        this.queryParams1.total = response.total;
      });
    },
    /** 入库记录按钮操作 */
    handleIncrease(row) {
      this.reset()
      this.PopUpsShow()
      const cpCodes = row.cpCode || this.ids
      increaseList(cpCodes).then(response => {
        this.increaseList=response.rows
        this.title = "入库记录";
        this.queryParams1.total = response.total;
      });
    },
    /** 库存详情按钮操作 */
    handleInventory(row) {
      this.reset()
      this.PopUpsShow()
      const cpCodes = row.cpCode || this.ids
      inventoryList(cpCodes).then(response => {
        this.inventoryList=response.rows
        this.title = "库存详情";
        this.queryParams1.total = response.total;
      });
    },
    /** 弹窗 */
    PopUpsShow(){
      this.open1=true
      this.loading = false;
    },
    /** 物料档案序号 */
    rowWldaMaterialFileIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 复选框选中数据 */
    handleWldaMaterialFileSelectionChange(selection) {
      this.checkedWldaMaterialFile = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      // this.download('warehouse/llrk/export', {
      //   ...this.queryParams
      // }, `llrk_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
